import { createSlice } from "@reduxjs/toolkit";

const userStore = createSlice({
  name: "user",
  initialState: {
    user: {},
  },
  // 同步方法
  reducers: {
    // 获取用户信息
    setUserInfo(state, action) {
      state.user = action.payload;
      localStorage.setItem("user", JSON.stringify(action.payload));
    },
    // 清除用户信息
    clearUserInfo(state) {
      state.user = {};
      localStorage.clear();
    },
  },
});

// 解构出actionCreater
const { setUserInfo, clearUserInfo } = userStore.actions;

const userReducer = userStore.reducer;

export { setUserInfo, clearUserInfo };

export default userReducer;
